
package com.example.testutil.util.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.text.TextUtils;

public class MD5Util {
    /*
     * 字符串加密
     */
    public static String calcMD5(String input) {

        if (TextUtils.isEmpty(input)) {
            return "";
        }
        byte[] data = input.getBytes();
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            return "";
        }
        // make MessageDigest
        md.update(data);
        byte[] hash = md.digest();
        // Converted to a string
        StringBuffer sbRet = new StringBuffer();
        for (int i = 0; i < hash.length; i++) {
            int v = hash[i] & 0xFF;
            if (v < 16)
                sbRet.append("0");
            sbRet.append(Integer.toString(v, 16));
        }

        return sbRet.toString();
    }

    /*
     * 计算File文件MD5
     */
    public static String md5(File file) {
        MessageDigest digest = null;
        FileInputStream fis = null;
        byte[] buffer = new byte[1024];

        try {
            if (!file.isFile() && !file.exists()) {
                return "";
            }

            digest = MessageDigest.getInstance("MD5");
            fis = new FileInputStream(file);

            while (true) {
                int len;
                if ((len = fis.read(buffer, 0, 1024)) == -1) {
                    fis.close();
                    break;
                }

                digest.update(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        BigInteger var5 = new BigInteger(1, digest.digest());
        return String.format("%1$032x", new Object[] {
            var5
        });
    }

    /**
     * Calculate MD5 code of the given file.
     * 
     * @param file File that need to be calculate.
     * @return MD5 code String.
     */
    public static String calcMD5(File file) {
        FileInputStream is = null;
        MessageDigest digest = null;
        byte[] buffer = new byte[1024];
        int length;
        try {
            digest = MessageDigest.getInstance("MD5");
            is = new FileInputStream(file);
            while ((length = is.read(buffer)) != -1) {
                digest.update(buffer, 0, length);
            }
            byte[] hash = digest.digest();
            StringBuffer sbRet = new StringBuffer();
            for (int i = 0; i < hash.length; i++) {
                int v = hash[i] & 0xFF;
                if (v < 16) {
                    sbRet.append(0);
                }
                sbRet.append(Integer.toString(v, 16));
            }
            return sbRet.toString();
        } catch (Exception e) {
            return null;
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
